package com.baidu.travel.timedtask.service.impl;

import com.baidu.travel.dao.CommentDao;
import com.baidu.travel.domain.Comment;
import com.baidu.travel.timedtask.dao.AutomaticCommentDao;
import com.baidu.travel.timedtask.dao.AutomaticCommentOrderDao;
import com.baidu.travel.timedtask.po.AutomaticCommentOrderPO;
import com.baidu.travel.timedtask.po.AutomaticCommentPO;
import com.baidu.travel.timedtask.service.AutomaticCommentService;
import com.baidu.travel.timedtask.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AutomaticCommentServiceImpl implements AutomaticCommentService {

    @Autowired
    private AutomaticCommentOrderDao automaticCommentServiceDao;

    @Autowired
    private AutomaticCommentDao automaticCommentDao;
    @Override
    public void automaticComment() {
        QueryWrapper<AutomaticCommentOrderPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ispay", "是");
        List<AutomaticCommentOrderPO> commentList = automaticCommentServiceDao.selectList(queryWrapper);
        commentList.forEach(po->{
            try {
                if (DateUtils.getCurrentTime().getTime()>DateUtils.parseDatetime(po.getTime()).getTime()+(24*60*60*7*1000)){
                    //查询出评论表中当前用户是否已经自动评论  再根据uid和自动评论的字段查询(设计表的时候没考虑到是否为自动评论，因此以自动评论字段作为是否为自动评论)
                    QueryWrapper<AutomaticCommentPO> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("uid", po.getUid());
                    queryWrapper1.eq("content", "推荐，感觉不错");
                    List<AutomaticCommentPO> automaticCommentPOList = automaticCommentDao.selectList(queryWrapper1);
                    if (automaticCommentPOList.size()!=0){
                        return;
                    }
                    AutomaticCommentPO automaticCommentPO = new AutomaticCommentPO();
                    automaticCommentPO.setUid(po.getUid());
                    automaticCommentPO.setRid(po.getRid());
                    automaticCommentPO.setTime(new Date());
                    automaticCommentPO.setContent("推荐，感觉不错");
                    automaticCommentDao.insert(automaticCommentPO);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
